const express=require("express")
const secret=express.Router();
const  {recommendTable:secretbase}=require("../database/secretbase");


// secret.use((req,res,next)=>{
//   if (req.signedCookies.ms_username=="admin") {
//     next();
//   } else {
//     res.clearCookie("ms_require")
//     res.clearCookie("ms_username")
//     res.send("error")
//   }
  
// })

secret.post('/del',async (req,res,next)=>{
    let data=await secretbase.remove({
        secretID:req.body.id
    })

    if (data.deletedCount) {
        res.send({
            code:0,
            msg:'删除成功'
        })
    }else{
        res.send({
            code:5000,
            msg:'该数据不存在'
        })
    }
})



secret.get('/:num',async (req,res,next)=>{
    let data = await secretbase.find().skip(Number(req.params.num));
    res.send(data)
})




secret.get('/',async (req,res,next)=>{
    let pwd = make(16,true)
    pwd =await findBase(pwd)
    secretbase.create({
        secretID:pwd
    },(err, doc, len)=>{
        if (err) {
            res.send({
                code:5000,
                msg:err
            })
        }else{
            res.send({
                code:0,
                msg:doc,
                len:len
            })
        }
    })
})


async function findBase(pwd) {
    let data =await secretbase.find({secretID:pwd})
    if (data.length) {
        createBase()
    }else{
        return pwd
    }
}

function createBase() {
    let pwd = make(16,true)
    findBase(pwd)
}

function createPw(length, punctuate) {
    let i;
    const punctuates = "()_-~@#$%^&*+=`[]{}:.,?";
    const pwArray = [];

    //随机生成字母
    for (i = 0; i < length; i++) {
        if (Math.round(Math.random()) === 0) {
            pwArray[i] = String.fromCharCode(Math.floor(Math.random() * 26) + 65);
        } else {
            pwArray[i] = String.fromCharCode(Math.floor(Math.random() * 26) + 97);
        }
    }
    //随机替换数字
    const maxNum = length - 2;
    const numLength = Math.floor(Math.random() * maxNum) + 1;
    for (i = 0; i < numLength; i++) {
        pwArray[Math.floor(Math.random() * length)] = Math.floor(Math.random() * 10);
    }
    //随机替换标点
    if (punctuate) {
        const punNum = punctuates.length;
        const maxPun = length - 3;
        const punLength = Math.floor(Math.random() * maxPun) + 1;
        for (i = 0; i < punLength; i++) {
            pwArray[Math.floor(Math.random() * length)] = punctuates.charAt(Math.floor(Math.random() * punNum));
        }
    }

    return pwArray.join("");
}
// 16  包含标点 (如：!, [, $, +等等)
function make(length, punctuate) {
    let pattern;
    if (punctuate) {
        eval("pattern = /^(?=.*[0-9].*)(?=.*[A-Z].*)(?=.*[a-z].*)(?=.*\\W.*).{" + length + "}$/;");
    } else {
        eval("pattern = /^(?=.*[0-9].*)(?=.*[A-Z].*)(?=.*[a-z].*).{" + length + "}$/;");
    }
    let pwStr = "";
    let check = !1;
    while (!check) {
        pwStr = createPw(length, punctuate);
        if (pattern.exec(pwStr)) {
            check = !0;
        }
    }
    return pwStr;
}


module.exports=secret